#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "bill.h"
#define BILL_FILE "bill.txt"

int write_bill(bill *r)
{
	FILE *fp;

	fp = fopen(BILL_FILE,"a");
	if (fp == NULL)
	{
		perror("打开bill.txt失败");
		return 1;
	}
	fprintf(fp,"%2d",r->roomId);
	fprintf(fp, "%-4d",r->price);
	fprintf(fp, "%-4d",r->yingPrice);
	fprintf(fp, "%-4d",r->realPrice);
	fprintf(fp, "%-4d",r->cashPledge);
	fprintf(fp, "%-4d",r->change);
	fprintf(fp, "%1d",r->status);
	fprintf(fp, "%-12s",r->inDay);
	fprintf(fp, "%-12s\n",r->outDay);
	if (fp)
		fclose(fp);
	return 0;

}

int write_bill_all(bill *r)
{
	FILE *fp;

	fp = fopen(BILL_FILE,"a");
	if (fp == NULL)
	{
		perror("打开bill.txt失败");
		return 1;
	}
	while(r->next)
	{
		fprintf(fp,"%2d",r->roomId);
		fprintf(fp, "%-4d",r->price);
		fprintf(fp, "%-4d",r->yingPrice);
		fprintf(fp, "%-4d",r->realPrice);
		fprintf(fp, "%-4d",r->cashPledge);
		fprintf(fp, "%-4d",r->change);
		fprintf(fp, "%1d",r->status);
		fprintf(fp, "%-12s",r->inDay);
		fprintf(fp, "%-12s\n",r->outDay);
		r = r->next;
	}
	if (fp)
		fclose(fp);
	return 0;

}

bill* read_bill()
{
	bill *head,*p,*newNode;
	bill *tp;
	FILE *fp;
	fp = fopen(BILL_FILE,"r");
	if (fp == NULL)
	{
		perror("open bill file read");
		return NULL;
	}
	head = (bill *)malloc(sizeof(bill));
	if (NULL == head)
	{
		printf("malloc fail\n");
		if(fp) fclose(fp);
		return NULL;
	}
	memset(head,0,sizeof(bill));	
	p = head;

	while(!feof(fp))
	{
		fscanf(fp, "%2d",&p->roomId);
		fscanf(fp, "%4d",&p->price);
		fscanf(fp, "%4d",&p->yingPrice);
		fscanf(fp, "%4d",&p->realPrice);
		fscanf(fp, "%4d",&p->cashPledge);
		fscanf(fp, "%4d",&p->change);
		fscanf(fp, "%1d",&p->status);
		fscanf(fp, "%12s",p->inDay);
		fscanf(fp, "%12s",p->outDay);

		newNode = (bill*)malloc(sizeof(bill));
		memset(newNode,0,sizeof(bill));
		p->next = newNode;
		tp = p;
		p = newNode;
	}
	tp->next = NULL;
	free(p);
	if(fp) fclose(fp);
	return head;
}

int display_bill(bill *head)
{
	bill *p;
	int num = 1;
	p = head;

	printf("##########################\n");
	printf("Num  RoomId Price YingPrice RealPrice CashPledge change Status inDay       outDay\n");
	while(p->next)
	{	
		printf("%-2d.  %-2d     %-4d  %-4d      %-4d      %-4d       %-4d   %d      %-12s%-12s\n",num,p->roomId,p->price,p->yingPrice,p->realPrice,p->cashPledge,p->change,p->status,p->inDay,p->outDay);
		p = p->next;
		num++;
	}
	printf("##########################\n");
	return 0;
}

int find_bill_roomId()
{
	bill *head,*p;
	int num = 1,s;
	head = read_bill();
	p = head;

	printf("请输入要查找账单的房间号:\n");
	scanf("%d",&s);	
	getchar();


	printf("##########################\n");
	printf("Num  RoomId Price YingPrice RealPrice CashPledge change Status inDay       outDay\n");
	while(p->next)
	{	
		if(p->roomId == s)
			{
				printf("%-2d.  %-2d     %-4d  %-4d      %-4d      %-4d       %-4d   %d      %-12s%-12s\n",num,p->roomId,p->price,p->yingPrice,p->realPrice,p->cashPledge,p->change,p->status,p->inDay,p->outDay);
				num++;
			}
		p = p->next;
	}
	printf("##########################\n");
	return 0;

}

int find_bill_inDay()
{
	bill *head,*p;
	int num = 1;
	char s[12];
	head = read_bill();
	p = head;

	printf("请输入要查找账单的开房时间:\n");
	scanf("%s",s);	
	getchar();


	printf("##########################\n");
	printf("Num  RoomId Price YingPrice RealPrice CashPledge change Status inDay       outDay\n");
	while(p->next)
	{	
		if(!strcmp(p->inDay,s))
			{
				printf("%-2d.  %-2d     %-4d  %-4d      %-4d      %-4d       %-4d   %d      %-12s%-12s\n",num,p->roomId,p->price,p->yingPrice,p->realPrice,p->cashPledge,p->change,p->status,p->inDay,p->outDay);
				num++;
			}
		p = p->next;
	}
	printf("##########################\n");
	return 0;
}

int check_out_rz()
{
	bill *pr;
	pr = (bill*)malloc(sizeof(bill));
	if( pr == NULL )
	{
		printf("malloc fail\n");
		return 1;
	}
	memset(pr,0,sizeof(bill));	

	printf("请重新输入入住的房间号以确认:\n");
	scanf("%d",&pr->roomId);
	getchar();
	printf("请输入房价:\n");
	scanf("%d",&pr->price);
	getchar();
	printf("请输入应收费用:\n");
	scanf("%d",&pr->yingPrice);
	getchar();
	printf("请输入实收费用:\n");
	scanf("%d",&pr->realPrice);
	getchar();
	printf("请输入押金:\n");
	scanf("%d",&pr->cashPledge);
	getchar();
	printf("请输入找零:\n");
	scanf("%d",&pr->change);
	getchar();
//	printf("请输入状态:\n");
//	printf("1表示入住押金 2表示预约押金 0表示已还押金\n");
//	scanf("%d",&pr->status);
//	getchar();
	pr->status = 1;
	printf("请输入开房时间:\n");
	fgets(pr->inDay,11,stdin);
	printf("请输入退房时间:\n");
	fgets(pr->outDay,11,stdin);

	write_bill(pr);
	free(pr);

	return 0;
}

int check_out_yy()
{
	bill *pr;
	pr = (bill*)malloc(sizeof(bill));
	if( pr == NULL )
	{
		printf("malloc fail\n");
		return 1;
	}
	memset(pr,0,sizeof(bill));	

	printf("请重新输入入住的房间号以确认:\n");
	scanf("%d",&pr->roomId);
	getchar();
	printf("请输入房价:\n");
	scanf("%d",&pr->price);
	getchar();
	printf("请输入应收费用:\n");
	scanf("%d",&pr->yingPrice);
	getchar();
	printf("请输入实收费用:\n");
	scanf("%d",&pr->realPrice);
	getchar();
	printf("请输入押金:\n");
	scanf("%d",&pr->cashPledge);
	getchar();
	printf("请输入找零:\n");
	scanf("%d",&pr->change);
	getchar();
	pr->status = 2;
	printf("请输入开房时间:\n");
	fgets(pr->inDay,11,stdin);
	printf("请输入退房时间:\n");
	fgets(pr->outDay,11,stdin);

	write_bill(pr);
	free(pr);
	
	return 0;
}

int check_out_hf()
{
	bill *pr;
	pr = (bill*)malloc(sizeof(bill));
	if( pr == NULL )
	{
		printf("malloc fail\n");
		return 1;
	}
	memset(pr,0,sizeof(bill));	

	printf("请重新输入要调换的房间号:\n");
	scanf("%d",&pr->roomId);
	getchar();
	printf("请输入房价:\n");
	scanf("%d",&pr->price);
	getchar();
	printf("请输入应收费用:\n");
	scanf("%d",&pr->yingPrice);
	getchar();
	printf("请输入实收费用:\n");
	scanf("%d",&pr->realPrice);
	getchar();
	printf("请输入押金:\n");
	scanf("%d",&pr->cashPledge);
	getchar();
	printf("请输入找零:\n");
	scanf("%d",&pr->change);
	getchar();
//	printf("请输入状态:\n");
//	printf("1表示入住押金 2表示预约押金 0表示已还押金\n");
//	scanf("%d",&pr->status);
//	getchar();
	pr->status = 1;
	printf("请输入开房时间:\n");
	fgets(pr->inDay,11,stdin);
	printf("请输入退房时间:\n");
	fgets(pr->outDay,11,stdin);

	write_bill(pr);
	free(pr);

	return 0;
}

int keep_room_bill()
{
	bill *pr;
	pr = (bill*)malloc(sizeof(bill));
	if( pr == NULL )
	{
		printf("malloc fail\n");
		return 1;
	}
	memset(pr,0,sizeof(bill));	

	printf("请重新输入要续住的房间号:\n");
	scanf("%d",&pr->roomId);
	getchar();
	printf("请输入房价:\n");
	scanf("%d",&pr->price);
	getchar();
	printf("请输入应收费用:\n");
	scanf("%d",&pr->yingPrice);
	getchar();
	printf("请输入实收费用:\n");
	scanf("%d",&pr->realPrice);
	getchar();
	printf("请输入押金:\n");
	scanf("%d",&pr->cashPledge);
	getchar();
	printf("请输入找零:\n");
	scanf("%d",&pr->change);
	getchar();
//	printf("请输入状态:\n");
//	printf("1表示入住押金 2表示预约押金 0表示已还押金\n");
//	scanf("%d",&pr->status);
//	getchar();
	pr->status = 1;
	printf("请输入开房时间:\n");
	fgets(pr->inDay,11,stdin);
	printf("请输入退房时间:\n");
	fgets(pr->outDay,11,stdin);

	write_bill(pr);
	free(pr);
	printf("旅客续住成功！\n");

	return 0;
}